package com.woniuxy.juc.semaphore;

import com.woniuxy.untis.Sleep;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Semaphore;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/4/12:10:55
 * <P> Description:
 */
@Slf4j(topic = "c.c")
@SuppressWarnings("all")
public class SemaphoreTest {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < 10; i++) {
            //TODO 获得semaphore
            new Thread(() -> {
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                try {
                    log.debug("running");
                    Sleep.sleep(1L);
                    log.debug("end...");
                }finally {
                    semaphore.release();
                }

            }, "t" + i).start();

        }
    }
}
